<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\common\Util;
use plugin\admin\app\constant\v1\CommonConstant;
use plugin\admin\app\model\V1OrderNode;
use support\Request;
use support\Response;
use plugin\admin\app\controller\Crud;
use support\Db;
use support\exception\BusinessException;

/**
 * 静态游戏销售
 */
class V1GameOrderController extends Crud
{
    /**
     * 无需登录及鉴权的方法
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 需要登录无需权的方法
     * @var array
     */
    protected $noNeedAuth = [
        'select'
    ];

    /**
     * @var V1OrderNode
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new V1OrderNode;
    }

    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('v1-game-order/index');
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select(Request $request): Response
    {
        $db = Util::db();
        $items = $this->model
            ->where('deleted', CommonConstant::DELETE_0)
            ->whereIn('refund_status', [CommonConstant::DEFAULT_0, CommonConstant::DEFAULT_1,CommonConstant::DEFAULT_3])
            ->select('game_id', $db->raw('COUNT(id) as number'))
            ->groupBy('game_id')
            ->orderBy('number','desc')
            ->get();
        return json(['code' => 0, 'msg' => 'ok', 'count' => 0, 'data' => $items]);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('v1-region-area/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('v1-region-area/update');
    }

}
